<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Crypt;



class LoginController extends Controller
{
    public function index()
    {
    	return view('admin.login.login');
    }

    public function dologin(Request $request)
    {

    	$data=$request->except('_token');
    	// return($request->captcha,)
        

        //验证验证码
    	if($request->captcha!=session('milkcaptcha'))
    	{
    		return back()->with(['info'=>'验证码不正确']);
    	}
        //验证填值
            $this->validate($request,[
                        'name'=>'required',
                        'password'=>'required'
                        ],[
                        'name.required'=>'用户名不为空',
                        'password.required'=>'密码不为空',
                        ]);
         //数据库取值验证密码
    	$res=DB::table('manager')->where('name',$request->name)->first();
        $pass=Crypt::decrypt($res->password);

        //密码正确进入 错误返回登录
        if($request->password==$pass)
        {
        session(['master'=>$res]);
        //写入记录表
        $re['name']=$res->name;
        $re['time']=time();
        DB::table('record')->insert($re);
        return redirect('admin/index');
        }else
        {
            return back()->with(['info'=>'账号或密码错误，请从新登录']);
        }

        
    }

    public function logout()
    {
    	session()->forget('master');
    	return redirect('admin/login');
    }

}
